package com.sctvcloud.longchang.ui.adapter.holder;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.gridsum.tracker.GridsumWebDissector;
import com.iflytek.cloud.thirdparty.V;
import com.ruihang.generalibrary.ui.util.ViewUtils;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.longchang.R;
import com.sctvcloud.longchang.application.Cache;
import com.sctvcloud.longchang.application.Constances;
import com.sctvcloud.longchang.beans.NewsItem;
import com.sctvcloud.longchang.ui.utils.GlideUtil;
import com.sctvcloud.longchang.ui.utils.SkipUtil;
import com.sctvcloud.longchang.ui.utils.UIUtils;
import com.sctvcloud.longchang.utils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 24小时新闻条目
 * Created by Administrator on 2017/7/5.
 */

public class News24Holder extends BaseAbsHolder<NewsItem> implements View.OnClickListener {
    @BindView(R.id.normal_layout)
    protected ConstraintLayout normalLayout;
    @BindView(R.id.item_news_dot)
    protected View dot;
    @BindView(R.id.item_news_line_top)
    protected View topLine;
    @BindView(R.id.item_news_line_bottom)
    protected View bottomLine;
    @BindView(R.id.item_news_divider)
    protected View divider;
    @BindView(R.id.item_news_image)
    protected CustomEXImageView img;
    @BindView(R.id.item_news_title)
    protected CustomFontTextView title;
    @BindView(R.id.item_news_views)
    protected CustomFontTextView readviews;
    @BindView(R.id.item_news_24_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_news_date)
    protected CustomFontTextView date;
    @BindView(R.id.item_news_mode_tag)
    protected CustomFontTextView liveTag;
    @BindView(R.id.item_news_tag)
    protected CustomFontTextView tag;
    @BindView(R.id.item_news_24_length)
    protected CustomFontTextView length;
    @BindView(R.id.item_news_24_media)
    protected ImageView media;

    @BindView(R.id.item_big_news)
    protected LinearLayout itemBigLayout;
    @BindView(R.id.item_news_big_dot)
    protected View bigDot;
    @BindView(R.id.item_news_line_big_top)
    protected View bigTopLine;
    @BindView(R.id.item_news_line_big_bottom)
    protected View bigBottomLine;
    @BindView(R.id.item_news_big_image)
    protected CustomEXImageView bigImg;
    @BindView(R.id.item_news_big_title)
    protected CustomFontTextView bigNewsTitle;
    @BindView(R.id.item_news_big_date)
    protected CustomFontTextView bigNewsDate;
    @BindView(R.id.item_news_big_views)
    protected CustomFontTextView bigNewsViews;
    @BindView(R.id.item_news_24_big_media)
    protected ImageView bigMedia;
    @BindView(R.id.item_news_mode_big_tag)
    protected CustomFontTextView liveBigTag;
    @BindView(R.id.item_news_24_big_length)
    protected CustomFontTextView bigLength;

    @BindView(R.id.item_news_stick)
    protected CustomFontTextView item_news_stick;

    @BindView(R.id.item_news_stick_big)
    protected CustomFontTextView item_news_stick_big;


    @BindView(R.id.item_news_big_divider)
    protected View bigDivider;

    @BindView(R.id.li_nopic)
    protected LinearLayout li_nopic;
    private NewsItem temp;
    @BindView(R.id.item_news_divider_nopic)
    protected View item_news_divider_nopic;

    @BindView(R.id.item_news_title_nopic)
    protected CustomFontTextView item_news_title_nopic;
    @BindView(R.id.item_news_date_nopic)
    protected CustomFontTextView item_news_date_nopic;
    @BindView(R.id.item_news_views_nopic)
    protected CustomFontTextView item_news_views_nopic;

    @BindView(R.id.item_more_img_news)
    protected LinearLayout itemMoreImgNewsLayout;
    @BindView(R.id.item_more_img_dot)
    protected View itemMoreImgDot;
    @BindView(R.id.item_more_img_line_top)
    protected View moreImgTopLine;
    @BindView(R.id.item_more_img_line_bottom)
    protected View moreImgBottomLine;
    @BindView(R.id.item_more_img_image_1)
    protected CustomEXImageView moreImg1;
    @BindView(R.id.item_more_img_image_2)
    protected CustomEXImageView moreImg2;
    @BindView(R.id.item_more_img_image_3)
    protected CustomEXImageView moreImg3;
    @BindView(R.id.item_more_img_title)
    protected CustomFontTextView moreImgNewsTitle;
    @BindView(R.id.item_more_img_date)
    protected CustomFontTextView moreImgNewsDate;
    @BindView(R.id.item_more_img_views)
    protected CustomFontTextView moreImgNewsViews;
    @BindView(R.id.item_more_img_divider)
    protected View moreImgDivider;


    @BindView(R.id.item_big_ad)
    protected LinearLayout adBigLayout;

    @BindView(R.id.item_ad_big_image)
    protected CustomEXImageView bigAdImg;
    @BindView(R.id.item_ad_big_title)
    protected CustomFontTextView bigAdTitle;
    @BindView(R.id.big_ad_resource)
    protected CustomFontTextView bigAdResource;
    @BindView(R.id.item_ad_big_divider)
    protected View bigAdDivider;
    @BindView(R.id.big_ad_resource_layout)
    protected LinearLayout adBigResourceLayout;

    @BindView(R.id.item_small_ad)
    protected LinearLayout adSmallLayout;

    @BindView(R.id.item_ad_small_image)
    protected CustomEXImageView smallAdImg;
    @BindView(R.id.item_ad_small_title)
    protected CustomFontTextView smallAdTitle;
    @BindView(R.id.small_ad_resource)
    protected CustomFontTextView smallAdResource;
    @BindView(R.id.item_ad_small_divider)
    protected View smallAdDivider;
    @BindView(R.id.small_ad_resource_layout)
    protected LinearLayout adSmallResourceLayout;


    private OnHomeNewsADListener adClickListener;

    private boolean isShowReadCount = true;

    public News24Holder(Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        isShowReadCount = true;
    }

    public News24Holder(Context context, View itemView, boolean isShowReadCount) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.isShowReadCount = isShowReadCount;
    }

    public News24Holder(Context context, View itemView, OnHomeNewsADListener listener) {
        super(context, itemView);
        this.context = context;
        adClickListener = listener;
        itemView.setOnClickListener(this);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(NewsItem t) {
        temp = t;
        if (t.getNewsType() == Constances.NEWS_TYPE_SMALL_AD) {
            adSmallLayout.setVisibility(View.VISIBLE);
            adBigLayout.setVisibility(View.GONE);
            normalLayout.setVisibility(View.GONE);
            itemBigLayout.setVisibility(View.GONE);
            li_nopic.setVisibility(View.GONE);
            GlideUtil.getGlid(context, t.getDataImg()).centerCrop().diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(smallAdImg);
            smallAdTitle.setText(t.getNewsTitle());
            smallAdResource.setText(t.getAdvertSource());
            if (t.getAfterTitleMode() > 0) {
                if (t.getAfterTitleMode() == 1) {
                    ViewUtils.setViewsGone(smallAdDivider);
                } else if (t.getAfterTitleMode() == 2) {
                    ViewUtils.setViewsGone(smallAdDivider);
                }
            } else {
                ViewUtils.setViewsVisible(smallAdDivider);
            }
            if (null != adClickListener) {
                adClickListener.OnNewsADLoad(t);
            }
            if ("0".equalsIgnoreCase(t.getIfDisplayAdvertisingWords())) {
                adSmallResourceLayout.setVisibility(View.VISIBLE);
            } else {
                adSmallResourceLayout.setVisibility(View.GONE);
            }
        } else if (t.getNewsType() == Constances.NEWS_TYPE_BIGGER_AD) {
            adBigLayout.setVisibility(View.VISIBLE);
            adSmallLayout.setVisibility(View.GONE);
            normalLayout.setVisibility(View.GONE);
            itemBigLayout.setVisibility(View.GONE);
            li_nopic.setVisibility(View.GONE);
            GlideUtil.getGlid(context, t.getDataImg()).centerCrop().diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(bigAdImg);
            bigAdTitle.setText(t.getNewsTitle());
            bigAdResource.setText(t.getAdvertSource());
            if (t.getAfterTitleMode() > 0) {
                if (t.getAfterTitleMode() == 1) {
                    ViewUtils.setViewsGone(bigAdDivider);
                } else if (t.getAfterTitleMode() == 2) {
                    ViewUtils.setViewsGone(bigAdDivider);
                }
            } else {
                ViewUtils.setViewsVisible(bigAdDivider);
            }
            if (null != adClickListener) {
                adClickListener.OnNewsADLoad(t);
            }
            if ("0".equalsIgnoreCase(t.getIfDisplayAdvertisingWords())) {
                adBigResourceLayout.setVisibility(View.VISIBLE);
            } else {
                adBigResourceLayout.setVisibility(View.GONE);
            }
        } else {
            if (TextUtils.isEmpty(t.getDataImg())) {
                adSmallLayout.setVisibility(View.GONE);
                adBigLayout.setVisibility(View.GONE);
                normalLayout.setVisibility(View.GONE);
                itemBigLayout.setVisibility(View.GONE);
                li_nopic.setVisibility(View.VISIBLE);
                itemMoreImgNewsLayout.setVisibility(View.GONE);
                item_news_title_nopic.setText(t.getDataTitle());
                item_news_date_nopic.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
                item_news_views_nopic.setText(UIUtils.getReadingNumber(t.getReadCount()));

                if (t.getAfterTitleMode() > 0) {
                    if (t.getAfterTitleMode() == 1) {
                        ViewUtils.setViewsGone(item_news_divider_nopic);
                    } else if (t.getAfterTitleMode() == 2) {
                        ViewUtils.setViewsGone(item_news_divider_nopic);
                    }
                } else {
                    ViewUtils.setViewsVisible(item_news_divider_nopic);
                }
                if (isShowReadCount) {
                    item_news_views_nopic.setVisibility(View.VISIBLE);
                } else {
                    item_news_views_nopic.setVisibility(View.GONE);
                }
            } else if ("3".equalsIgnoreCase(temp.getDisplayAmount())) {
                normalLayout.setVisibility(View.GONE);
                itemBigLayout.setVisibility(View.GONE);
                adSmallLayout.setVisibility(View.GONE);
                adBigLayout.setVisibility(View.GONE);
                li_nopic.setVisibility(View.GONE);
                itemMoreImgNewsLayout.setVisibility(View.VISIBLE);

                GlideUtil.loadImgSmall169Def(context, t.getNewsImage(), moreImg1);
                GlideUtil.loadImgSmall169Def(context, t.getSmallImage2(), moreImg2);
                GlideUtil.loadImgSmall169Def(context, t.getSmallImage3(), moreImg3);
                moreImgNewsTitle.setText(t.getDataTitle());
                moreImgNewsDate.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
                moreImgNewsViews.setText(UIUtils.getReadingNumber(t.getReadCount()));
                if (isShowReadCount) {
                    moreImgNewsViews.setVisibility(View.VISIBLE);
                } else {
                    moreImgNewsViews.setVisibility(View.GONE);
                }

                if (t.getAfterTitleMode() > 0) {
                    if (t.getAfterTitleMode() == 1) {
                        ViewUtils.setViewsGone(moreImgDivider);
                        moreImgTopLine.setVisibility(View.INVISIBLE);
                    } else if (t.getAfterTitleMode() == 2) {
                        ViewUtils.setViewsGone(moreImgDivider);
                    }
                } else {
                    ViewUtils.setViewsVisible(itemMoreImgDot, moreImgDivider, moreImgTopLine);
                }

            } else if (temp.getIsBigPic() == 1) {
                //大图显示
                normalLayout.setVisibility(View.GONE);
                itemBigLayout.setVisibility(View.VISIBLE);
                li_nopic.setVisibility(View.GONE);
                adSmallLayout.setVisibility(View.GONE);
                adBigLayout.setVisibility(View.GONE);
                itemMoreImgNewsLayout.setVisibility(View.GONE);
                GlideUtil.loadImgSmall169Def(context, t.getDataImg(), bigImg);

                bigNewsTitle.setText(t.getDataTitle());
                bigNewsDate.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
                bigNewsViews.setText(UIUtils.getReadingNumber(t.getReadCount()));

                if (isShowReadCount) {
                    bigNewsViews.setVisibility(View.VISIBLE);
                } else {
                    bigNewsViews.setVisibility(View.GONE);
                }
                if (t.isLiving()) {
                    ViewUtils.setViewsGone(bigLength, bigMedia);
                    liveBigTag.setVisibility(View.VISIBLE);
                } else {
                    liveBigTag.setVisibility(View.GONE);
                    if (t.getDataType() > 1) {
                        ViewUtils.setViewVisible(bigMedia);
                        if (TextUtils.isEmpty(t.getDataMediaLength())) {
                            bigLength.setVisibility(View.GONE);
                        } else {
                            String len = TimeUtils.formatTimeToHM(t.getDataMediaLength());
                            if (!TextUtils.isEmpty(len)) {
                                bigLength.setVisibility(View.VISIBLE);
                                bigLength.setText(len);
                            } else {
                                bigLength.setVisibility(View.GONE);
                            }
                        }
                    } else {
                        ViewUtils.setViewsGone(bigLength, bigMedia);
                    }
                }
                bigLength.setVisibility(View.GONE);
                if (t.getAfterTitleMode() > 0) {
                    if (t.getAfterTitleMode() == 1) {
                        ViewUtils.setViewsGone(bigDivider);
                        bigTopLine.setVisibility(View.INVISIBLE);
                    } else if (t.getAfterTitleMode() == 2) {
                        ViewUtils.setViewsGone(bigDivider);
                    }
                } else {
                    ViewUtils.setViewsVisible(bigDot, bigDivider, bigTopLine);
                }
                if (t.getNewsType() == 2) { //音频
                    bigMedia.setImageResource(R.mipmap.icon_voice);
                    bigMedia.setVisibility(View.VISIBLE);
                } else if (t.getNewsType() == 3) {   //视频
                    bigMedia.setImageResource(R.mipmap.icon_play);
                    bigMedia.setVisibility(View.VISIBLE);
                } else {
                    bigMedia.setVisibility(View.GONE);
                }


                if (t.getIsStick() == 1) {
                    item_news_stick_big.setVisibility(View.VISIBLE);
                } else {
                    item_news_stick_big.setVisibility(View.GONE);
                }


            } else {
                normalLayout.setVisibility(View.VISIBLE);
                itemBigLayout.setVisibility(View.GONE);
                li_nopic.setVisibility(View.GONE);
                adSmallLayout.setVisibility(View.GONE);
                adBigLayout.setVisibility(View.GONE);
                itemMoreImgNewsLayout.setVisibility(View.GONE);
                if (t.getItemMode() > 0) {
                    dot.setVisibility(View.GONE);
                    if (t.getItemMode() == 1) {
                        topLine.setVisibility(View.GONE);
                    } else {
                        topLine.setVisibility(View.VISIBLE);
                    }
                    divider.setVisibility(View.GONE);
                } else {
                    if (dot.getVisibility() != View.VISIBLE) {
                        dot.setVisibility(View.VISIBLE);
                        topLine.setVisibility(View.VISIBLE);
                        divider.setVisibility(View.VISIBLE);
                    }
                }
                if (t.isLiving()) {
                    ViewUtils.setViewsGone(length, media);
                    liveTag.setVisibility(View.VISIBLE);
                } else {
                    liveTag.setVisibility(View.GONE);
                    if (t.getDataType() > 1) {
                        ViewUtils.setViewVisible(media);
                        if (TextUtils.isEmpty(t.getDataMediaLength())) {
                            length.setVisibility(View.GONE);
                        } else {
                            length.setVisibility(View.VISIBLE);
                            length.setText(TimeUtils.formatTimeToHM(t.getDataMediaLength()));
                        }
                    } else {
                        ViewUtils.setViewsGone(length, media);
                    }
                }
                length.setVisibility(View.GONE);
                if (t.getAfterTitleMode() > 0) {
                    if (t.getAfterTitleMode() == 1) {
                        ViewUtils.setViewsGone(dot, divider, topLine);
                    } else if (t.getAfterTitleMode() == 2) {
                        ViewUtils.setViewsGone(dot, divider);
                    }
                } else {
                    ViewUtils.setViewsVisible(dot, divider, topLine);
                }
                String timeStr = DateUtils.getDateHourString(DateUtils.getDataDate(t));
                time.setText(timeStr);
                date.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
                //t.setReadCount(1000000);
                readviews.setText(UIUtils.getReadingNumber(t.getReadCount()));
                if (isShowReadCount) {
                    readviews.setVisibility(View.VISIBLE);
                } else {
                    readviews.setVisibility(View.GONE);
                }
                title.setText(t.getDataTitle());
                if (!TextUtils.isEmpty(t.getDataTag())) {
                    tag.setText(t.getDataTag());
                }
                tag.setVisibility(View.INVISIBLE);


                if (t.getIsStick() == 1) {
                    item_news_stick.setVisibility(View.VISIBLE);
                } else {
                    item_news_stick.setVisibility(View.GONE);
                }

                GlideUtil.loadImgSmall169Def(context, t.getDataImg(), img);
                if (t.getNewsType() == 2) { //音频
                    media.setImageResource(R.mipmap.icon_home_voice);
                    media.setVisibility(View.VISIBLE);
                } else if (t.getNewsType() == 3) {   //视频
                    media.setImageResource(R.mipmap.icon_home_play);
                    media.setVisibility(View.VISIBLE);
                } else {
                    media.setVisibility(View.GONE);
                }
            }
        }
        //区县，隐藏时间线
        topLine.setVisibility(View.GONE);
        dot.setVisibility(View.GONE);
        bottomLine.setVisibility(View.GONE);
        bigTopLine.setVisibility(View.GONE);
        bigBottomLine.setVisibility(View.GONE);
        bigDot.setVisibility(View.GONE);
        moreImgTopLine.setVisibility(View.GONE);
        moreImgBottomLine.setVisibility(View.GONE);
        itemMoreImgDot.setVisibility(View.GONE);

    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
        if (mode == ITEMMODE_LAST) {
            bottomLine.setVisibility(View.GONE);
            bigBottomLine.setVisibility(View.GONE);
            moreImgBottomLine.setVisibility(View.GONE);
        } else {
//            bottomLine.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "精选"));
        pairs.add(new Pair<>("位置", "24小时"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getNewsTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getNewsTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }
        SkipUtil.skipWithNewsItem(context, temp, map);
        if (temp.getNewsType() == Constances.NEWS_TYPE_SMALL_AD
                || temp.getNewsType() == Constances.NEWS_TYPE_BIGGER_AD) {
            if (null != adClickListener
                    && (temp.getAdvertType().equalsIgnoreCase("0") || temp.getAdvertType().equalsIgnoreCase("2"))) {
                //广告点击详情统计
                adClickListener.OnNewsADLoadInfo(temp);
            }
        }
    }

    public interface OnHomeNewsADListener {
        void OnNewsADLoad(NewsItem item);

        void OnNewsADLoadInfo(NewsItem item);
    }
}
